package com.example.book;

import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BookDaoImpl Bookdao=new BookDaoImpl();
        String title,author;
        int id=0;
        double price;
        while (true){
            System.out.println("===============================================================");
            System.out.println("选择您想要的操作:");
            System.out.println("1:新增图书");
            System.out.println("2:更新图书");
            System.out.println("3:删除图书");
            System.out.println("4:查询图书");
            System.out.println("5:查询全部图书");
            System.out.println("6:退出");
            System.out.println("===============================================================");
            System.out.println("您的操作是:");
            String flag=sc.next();
            switch (flag){
                case "1":
                    System.out.println("请输入书名:");
                    title = sc.next();
                    Pattern pattern = Pattern.compile("《(.*?)》");
                    Matcher matcher = pattern.matcher(title);
                    if (matcher.find()){
                        title=title;
                    }
                    else {
                        title="《"+title+"》";
                    }
                    System.out.println("请输入作者:");
                    author = sc.next();
                    System.out.println("请输入价格:");
                    price = sc.nextDouble();
                    Bookdao.addBook(new Book(id,title,author,price));
                    break;
                case "2":
                    System.out.println("请输入id:");
                    id = sc.nextInt();
                    Book book = Bookdao.getById(id);
                    if(book==null){
                        System.out.println("id不存在，请重输");
                    }
                    else {
                        System.out.println("请输入书名:");
                        title = sc.next();
                        Pattern pattern1 = Pattern.compile("《(.*?)》");
                        Matcher matcher1 = pattern1.matcher(title);
                        if (matcher1.find()){
                            title=title;
                        }
                        else {
                            title="《"+title+"》";
                        }
                        System.out.println("请输入作者:");
                        author = sc.next();
                        System.out.println("请输入价格:");
                        price = sc.nextDouble();
                        Bookdao.updateBook(new Book(id,title,author,price));
                    }
                    break;
                case "3":
                    System.out.println("请输入id:");
                    id = sc.nextInt();
                    Book book1 = Bookdao.getById(id);
                    if (book1==null){
                        System.out.println("id不存在，请重新输入");
                    }
                    else {
                        Bookdao.delBook(id);
                    }
                    break;
                case "4":
                    System.out.println("请输入查询id:");
                    id = sc.nextInt();
                    Book book2 = Bookdao.getById(id);
                    if (book2==null){
                        System.out.println("id不存在，请重新输入");
                    }
                    else {
                        System.out.println(book2);
                    }
                    break;
                case "5":
                    List<Book> booklist = Bookdao.getAllBooks();
                    for(Book book3:booklist){
                        System.out.println(book3);
                    };
                    break;
                case "6":
                    System.out.println("退出系统成功");
                    sc.close();
                    return;
                default:
                    System.out.println("输入错误，请重新输入！\n");
            }

        }

    }

}
